<?php
declare (strict_types=1);

namespace app\course\service\coupon\admin;

use app\common\exception\LogicException;
use app\common\library\Excel;
use app\common\service\BaseService;
use app\course\logic\coupon\admin\CouponCard as CouponcardLogic;
use app\course\logic\coupon\admin\CouponOffline as CouponOfflineLogic;
use app\course\logic\coupon\admin\CouponUser as CouponUserLogic;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageForm;
use eduline\admin\page\PageList;
use think\facade\Request;

/**
 * 优惠券
 */
class CouponCard extends BaseService
{
    /**
     * 卡券列表
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-13
     *
     * @return   [type]                         [description]
     */
    public function index(int $online)
    {
        $map         = [];
        $is_platform = request()->is_platform;

        if ($is_platform) {
            Request::has('mhm_id', 'get', true) && $map['__TABLE__.mhm_id'] = Request::get('mhm_id/d');
        }

        Request::has('issue_no', 'get', true) && $map['__TABLE__.issue_no'] = Request::get('issue_no/s');

        $data = app(CouponcardLogic::class)->getList($map, $online);

        // 定义字段
        $keyList   = [
            'issue_no'      => ListItem::make()->title('期号'),
            'school_name'   => ListItem::make()->title('所属机构')->visible($is_platform),
            'coupon_info'   => ListItem::make('custom')->title('优惠信息'),
            'use_count'     => ListItem::make('custom')->title('卡券数量(已领取/总量)')->visible($online == 1),
            'offline_count' => ListItem::make('custom')->title('发放总量')->visible($online == 0),
            'derive_time'   => ListItem::make('custom')->title('领取时间')->visible($online == 1),
            'use_time'      => ListItem::make('custom')->title('使用时间'),
        ];
        $searchKey = [
            'mhm_id'   => FormItem::make('searchSchool')->title(false)->visible($is_platform),
            'issue_no' => FormItem::make('input')->title(false)->placeholder('期号')->maxlength(50),
        ];
        // 设置表单
        $list = app(PageList::class);
        // 表单字段
        $list->pageKey   = $keyList;
        $list->searchKey = $searchKey;
        $list->datas     = $data;

        return $list->send();
    }

    /**
     * 优惠券--用户领取以及使用情况
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-15
     *
     * @return   [type]                         [description]
     */
    public function details(int $coupon_id)
    {
        $data = app(CouponUserLogic::class)->getListByCouponId($coupon_id);

        // 定义字段
        $keyList = [
            'code'        => ListItem::make()->title('卡券编号'),
            'phone'       => ListItem::make()->title('领取用户'),
            'create_time' => ListItem::make()->title('领取时间'),
            'status_text' => ListItem::make()->title('使用状态'),
            'update_time' => ListItem::make()->title('操作时间'),
        ];
        // 设置表单
        $list = app(PageList::class);
        // 表单字段
        $list->pageKey = $keyList;

        $list->datas = $data;

        return $list->send();
    }

    /**
     * 实体卡详情页面
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-16
     *
     * @param int $coupon_id [description]
     * @return   [type]                                    [description]
     */
    public function offlineDetatils(int $coupon_id)
    {
        $data = app(CouponOfflineLogic::class)->getListByCouponId($coupon_id);

        // 定义字段
        $keyList = [
            'offline_code' => ListItem::make()->title('卡券编号'),
            'phone'        => ListItem::make()->title('使用用户'),
            'create_time'  => ListItem::make()->title('生成时间'),
            'status_text'  => ListItem::make()->title('使用状态'),
            'update_time'  => ListItem::make()->title('操作时间'),
        ];
        // 设置表单
        $list = app(PageList::class);
        // 表单字段
        $list->pageKey = $keyList;

        $list->datas = $data;

        return $list->send();
    }

    /**
     * 导出可用优惠券
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/02/11
     *
     * @return false|string|void
     */
    public function export()
    {
        $map         = [];
        $is_platform = request()->is_platform;

        if ($is_platform) {
            Request::has('mhm_id', 'post', true) && $map['mhm_id'] = Request::post('mhm_id/d');
        }

        Request::has('issue_no', 'post', true) && $map['issue_no'] = Request::post('issue_no/s');

        try {
            /** @var CouponOfflineLogic $couponOfflineLogic */
            $couponOfflineLogic = app(CouponOfflineLogic::class);
            $datas              = $couponOfflineLogic->getExportData(1, $map);
            // 表格列名
            $xlsCell = [
                ['offline_code', '卡号'],
                ['use_time', '使用时间'],
                ['use_stime', '开始使用时间'],
                ['use_etime', '结束使用时间'],
                ['maxprice', '满减条件'],
                ['price', '满减金额'],
            ];

            // 输出表格数据
            return Excel::exportExcel('课程实体卡', $xlsCell, $datas);
        } catch (LogicException $e) {
            throw new LogicException($e->getMessage());
        }
    }
}
